<?php

namespace Hilaoyu\LaravelExtend\Services\Realnames\Controllers;

use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelExtend\Services\Realnames\RealnamesEntity;
use Hilaoyu\LaravelExtend\Services\Realnames\RealnamesModelAbstract;
use Hilaoyu\LaravelPrivateApi\PrivateApiControllerAbstract;
use Illuminate\Http\Request;


class RealnameServiceController extends PrivateApiControllerAbstract
{


    public function save(Request $request)
    {

        $app = $request->route('_realname_app');
        if (!$app) {
            return comResponse()->failed('应用未授权')->json();
        }

        $data = $this->getRequestData();
        $data = (array)$data;
        $data['app_id'] = $app->id;


        try {

            $entity = new RealnamesEntity();

            $model = null;
            $user_id = data_get($data, 'user_id', '');
            if ($user_id && ($model_temp = $entity->find(['user_id'=>$user_id]))) {
                $model = $model_temp;
            }

            $model_tmp = $entity->save($data,$model);

            if ($model_tmp) {
                $model = $model_tmp;
            }else{
                comResponse()->withErrors($entity->getError());
                //throw new \Exception('保存数据出错 ');
            }

            $model = $this->filePathToAbsolute($model);

            $encryptor = $this->getEncryptor();

            comResponse()->success('保存成功')->with($model, null, $encryptor);
        } catch (\Exception $exception) {
            $error = $exception->getMessage();
            comResponse()->failed($error)->withErrors($error);
        }

        return comResponse()->json();
    }

    public function find(Request $request)
    {

        $app = $request->route('_realname_app');
        if (!$app) {
            return comResponse()->failed('应用未授权')->json();
        }

        $where = $this->getRequestData();


        try {

            $entity = new RealnamesEntity();

            $model = $entity->find($where);


            if (!$model) {
                throw new \Exception('数据不存在');
            }
            $model = $this->filePathToAbsolute($model);
            $encryptor = $this->getEncryptor();

            comResponse()->success('保存成功')->with($model, null, $encryptor);
        } catch (\Exception $exception) {
            $error = $exception->getMessage();
            comResponse()->failed($error)->withErrors($error);
        }

        return comResponse()->json();
    }


    public function cardTypeOptions(Request $request){
        $selected = $this->getRequestData('selected','');
        $add_title = $this->getRequestData('add_title','');

        try{
            $entity = new RealnamesEntity();

            $options = $entity->buildTypeOptions($selected,$add_title);

            $encryptor = $this->getEncryptor();

            comResponse()->success('查询成功')->with($options,null, $encryptor);
        }catch (\Exception $exception){
            comResponse()->failed($exception->getMessage());
        }

        return comResponse()->json();
    }

    public function uploaders(Request $request){

        try{

            $entity = new RealnamesEntity();
            $uploders = $entity->fileUploaders();
            $encryptor = $this->getEncryptor();

            comResponse()->success('查询成功')->with($uploders,null, $encryptor);
        }catch (\Exception $exception){
            comResponse()->failed($exception->getMessage());
        }

        return comResponse()->json();
    }
    public function userRealname(Request $request){

        $user_id = $this->getRequestData('user_id','');

        try{

            $entity = new RealnamesEntity();
            $model = null;
            if($user_id){
                $model = $entity->findUserRealname($user_id);
            }

            $model = $this->filePathToAbsolute($model);
            $encryptor = $this->getEncryptor();

            comResponse()->success('查询成功')->with($model,null, $encryptor);
        }catch (\Exception $exception){
            comResponse()->failed($exception->getMessage());
        }

        return comResponse()->json();
    }


    protected function filePathToAbsolute($model){
        if($model instanceof RealnamesModelAbstract){
            $card_img = $model->getAttribute('card_img');
            if($card_img){
                foreach ($card_img as $k => &$v){
                    if($v && ($FS = FileService::make($v))){
                        $v = $FS->url(true);
                    }
                }
                $model->setAttribute('card_img',$card_img);
            }

            $user_img = $model->getAttribute('user_img');
            if($user_img){
                foreach ($user_img as $k => &$v){
                    if($v && ($FS = FileService::make($v))){
                        $v = $FS->url(true);
                    }
                }
                $model->setAttribute('user_img',$user_img);
            }
        }

        return $model;
    }
}
